<?php

/**
 * RasterImageTest
 *
 * @author vbolshov
 */

use km\graphics\RasterImage;

use \PHPUnit_Framework_TestCase;

require_once dirname(__FILE__) . '/../../lib/km/graphics/RasterImageBackend.php';
require_once dirname(__FILE__) . '/../../lib/km/graphics/RasterImageBackendGD.php';
require_once dirname(__FILE__) . '/../../lib/km/graphics/RasterImage.php';

define('TEST_IMAGE', dirname(__FILE__) . '/images/aha.jpg');
list($w, $h) = getimagesize(TEST_IMAGE);
define('TEST_IMAGE_W', $w);
define('TEST_IMAGE_H', $h);

class RasterImageTest extends PHPUnit_Framework_TestCase {
	function testScale()
	{
		$ih = $this->createHandler();
		$ih->scale(50, 50);
		$this->assertEquals(50, $ih->getWidth());
		$this->assertEquals(50, $ih->getHeight());
	}
	function testScaleToWidth()
	{
		$ih = $this->createHandler();
		$new_width = floor(TEST_IMAGE_W / 2);
		$asserted_height = floor(TEST_IMAGE_H / 2);
		$ih->scaleToWidth($new_width);
		$this->assertEquals($new_width, $ih->getWidth());
		$this->assertEquals($asserted_height, $ih->getHeight());
	}
	function testScaleToHeight()
	{
		$ih = $this->createHandler();
		$new_height = floor(TEST_IMAGE_H / 2);
		$asserted_width = floor(TEST_IMAGE_W / 2);
		$ih->scaleToHeight($new_height);
		$this->assertEquals($new_height, $ih->getHeight());
		$this->assertEquals($asserted_width, $ih->getWidth());
	}
	function testScaleToMaximumSize()
	{
		$ih = $this->createHandler();
		
		$size = 50;
		$album_orientation = TEST_IMAGE_W > TEST_IMAGE_H;
		$new_width = ($album_orientation) ? $size : floor(TEST_IMAGE_W * 50 / TEST_IMAGE_H);
		$new_height = ($album_orientation) ? floor(TEST_IMAGE_H * 50 / TEST_IMAGE_W) : $size;
		
		$ih->scaleToMaxSize($size);
		$this->assertEquals($new_width, $ih->getWidth());
		$this->assertEquals($new_height, $ih->getHeight());
	}
	function testExactInscribing()
	{
		$ih = $this->createHandler();
		$ih->inscribeExact(50, 50, true, 0, 0, 0, 127);
		$this->assertEquals(50, $ih->getHeight());
		$this->assertEquals(50, $ih->getWidth());
	}
	function createHandler()
	{
		$ih = new RasterImage();
		$ih->loadFile(TEST_IMAGE);
		return $ih;
	}
}
